package com.ruoyi.miaomiao.controller.anonymous;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.miaomiao.domain.Trolley;
import com.ruoyi.miaomiao.domain.vo.TrolleyVO;
import com.ruoyi.miaomiao.service.ITrolleyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 服务于用户的购物车表crud的Controller
 */
@Api(tags = "用户购物车管理") //  tags：你可以当作是这个组的名字。
@RestController
@RequestMapping("/trolley")
@ApiResponses(value = {@ApiResponse(code = 0, message = "购物车对象与商品对象的集合", response = TrolleyVO.class),
        @ApiResponse(code = 0, message = "购物车对象信息", response = Trolley.class)})
public class AnTrolleyController extends BaseController
{
    @Autowired
    private ITrolleyService trolleyService;

    //  TODO: 以下crud都仍需要身份验证方法

    /**
     * 按用户的user_id查询购物车列表
     */
    @ApiOperation(value = "获取用户购物车列表",notes = "按用户id查询其购物车列表")
    @GetMapping(value = "/{userId}")
    public TableDataInfo listByUserId(@PathVariable("userId") Long userId)
    {
        startPage();
        // TODO: 需判断传进来的值是否为数字
        if(userId < 0)
        {
            return getDataTableError();
        }
        return getDataTable(trolleyService.selectTrolleyByUserId(userId));
    }

    /**
     * 新增购物车记录
     */
    @ApiOperation(value = "新增购物车记录",notes = "上传原生购物车对象类型数据并新增（不建议填写trolleyID字段，userID必填）")
    @PostMapping
    public AjaxResult add(@RequestBody Trolley trolley)
    {
        return toAjax(trolleyService.insertTrolley(trolley));
    }

    /**
     * 修改购物车记录
     */
    @ApiOperation(value = "修改购物车记录",notes = "上传原生购物车对象类型数据并修改（trolleyID字段与userID必填）")
    @PutMapping
    public AjaxResult edit(@RequestBody Trolley trolley)
    {
        return toAjax(trolleyService.updateTrolley(trolley));
    }

    /**
     * 删除购物车记录
     */
    @ApiOperation(value = "删除购物车记录",notes = "指定用户Id和购物车Id数组并删除购物车记录")
    @DeleteMapping("/{userId}/{trolleyIds}")
    public AjaxResult remove(@PathVariable("userId") Long userId, @PathVariable Long[] trolleyIds)
    {
        return toAjax(trolleyService.deleteTrolleyByUserAndTrolleyIds(userId, trolleyIds));
    }
}
